package com.daocao.hua.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.CardView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.daocao.hua.R;
import com.daocao.hua.api.callback.BankContract;
import com.daocao.hua.api.requestInfo.BankRequest;
import com.daocao.hua.constant.Const;
import com.daocao.hua.model.BankListModel;
import com.daocao.hua.view.BankViewItem;
import com.daocao.hua.view.CircularProgress;

/**
 * 银行卡界面
 * Created by Master.Dong on 16/4/10.
 */
public class BankActivity extends BaseActivity implements View.OnClickListener, BankContract.View {

    private BankViewItem bankViewItem;
    private CircularProgress loading;
    private CardView addView, bankView;

    private TextView name, type, cardno, addTv,changeView
            ;
    private ImageView icon;
    private String realName, idno;
    private BankRequest api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentView(R.layout.bank_activity);
        baseInitView(this, "BankActivity", Const.ViewType.ACTIVITY);
        initView();

    }

    private void initView() {
        api = BankRequest.create();
        api.attachView(this);
        setTitleText(getResources().getString(R.string.bank_card));
        setBackIntent();

        loading = (CircularProgress) findViewById(R.id.progressBar);
        addView = (CardView) findViewById(R.id.add_view);
        bankView = (CardView) findViewById(R.id.bank_view);
        changeView = (TextView) findViewById(R.id.change_view);
        name = (TextView) findViewById(R.id.name);
        type = (TextView) findViewById(R.id.type);
        cardno = (TextView) findViewById(R.id.cardno);
        addTv = (TextView) findViewById(R.id.add_tv);
        icon = (ImageView) findViewById(R.id.icon);

        addView.setOnClickListener(this);
        changeView.setOnClickListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        getBankList();
    }

    /**
     * 是否显示加载框
     *
     * @param b
     */
    private void showLoading(boolean b) {
        if (b) {
            if (loading != null) loading.setVisibility(View.VISIBLE);
        } else {
            if (loading != null) loading.setVisibility(View.GONE);
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.add_view:
            case R.id.change_view:
                Intent intent = new Intent(this, BankEditV2Activity.class);
                intent.putExtra("realName", realName);
                startActivity(intent);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                break;
        }
    }

    /**
     * 获取已绑定银行卡
     */
    private void getBankList() {
        api.getBankList();
    }

    @Override
    public void showError() {

    }

    @Override
    public void complete() {
        showLoading(false);
    }

    @Override
    public void onRequestStart() {
        showLoading(true);
    }

    @Override
    public void getBankListSuccess(BankListModel model) {
        realName = model.getReal_name();
        if (!TextUtils.isEmpty(realName))
            addTv.setText(getResources().getString(R.string.add_bank_text1) + realName.replace(realName.substring(0, 1), "*") + getResources().getString(R.string.add_bank_text2));

        if (model.getBanks().size() <= 0){
            addView.setVisibility(View.VISIBLE);
            return;
        }

        addView.setVisibility(View.GONE);
        changeView.setVisibility(View.VISIBLE);
        bankView.setVisibility(View.VISIBLE);

        name.setText(model.getBanks().get(0).getName());
        if (!TextUtils.isEmpty(model.getBanks().get(0).getCardtype()))
            type.setText(model.getBanks().get(0).getCardtype());
        cardno.setText(model.getBanks().get(0).getBankcard());
        if (!TextUtils.isEmpty(model.getReal_name())) realName = model.getReal_name();

        bankViewItem = new BankViewItem();
        int id = bankViewItem.getBankRes(model.getBanks().get(0).getName());
        icon.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), bankViewItem.bankImg[id]));
        bankView.setCardBackgroundColor(ContextCompat.getColor(getApplicationContext(), bankViewItem.bankColor[id]));

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        api.detachView();
        api = null;
    }
}
